package org.springcloud.plugin.strategy.service.configuration;

import org.springcloud.plugin.framework.strategy.adapter.DefaultDiscoveryEnabledAdapter;
import org.springcloud.plugin.framework.strategy.adapter.DiscoveryEnabledAdapter;
import org.springcloud.plugin.framework.strategy.constant.StrategyConstant;
import org.springcloud.plugin.strategy.service.constant.ServiceStrategyConstant;
import org.springcloud.plugin.strategy.service.filter.DefaultServiceStrategyRouteFilter;
import org.springcloud.plugin.strategy.service.filter.ServiceStrategyRouteFilter;
import org.springcloud.plugin.strategy.service.interceptor.FeignStrategyInterceptor;
import org.springcloud.plugin.strategy.service.interceptor.RestTemplateStrategyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * author: yanggld
 * date: 2019/12/22 16:26
 * version: 1.0
 * description: ServiceStrategyAutoConfiguration
 */
@Configuration
@AutoConfigureBefore(RibbonClientConfiguration.class)
public class ServiceStrategyAutoConfiguration {

    @Autowired
    private ConfigurableEnvironment environment;

    @Bean
    @ConditionalOnProperty(value = ServiceStrategyConstant.SPRING_APPLICATION_STRATEGY_REST_INTERCEPT_ENABLED, matchIfMissing = true)
    public RestTemplateStrategyInterceptor restTemplateStrategyInterceptor() {
        String contextRequestHeaders = environment.getProperty(ServiceStrategyConstant.SPRING_APPLICATION_STRATEGY_CONTEXT_REQUEST_HEADERS);
        String businessRequestHeaders = environment.getProperty(ServiceStrategyConstant.SPRING_APPLICATION_STRATEGY_BUSINESS_REQUEST_HEADERS);

        return new RestTemplateStrategyInterceptor(contextRequestHeaders, businessRequestHeaders);
    }

    @Bean
    @ConditionalOnProperty(value = ServiceStrategyConstant.SPRING_APPLICATION_STRATEGY_REST_INTERCEPT_ENABLED, matchIfMissing = true)
    public FeignStrategyInterceptor feignStrategyInterceptor() {
        String contextRequestHeaders = environment.getProperty(ServiceStrategyConstant.SPRING_APPLICATION_STRATEGY_CONTEXT_REQUEST_HEADERS);
        String businessRequestHeaders = environment.getProperty(ServiceStrategyConstant.SPRING_APPLICATION_STRATEGY_BUSINESS_REQUEST_HEADERS);

        return new FeignStrategyInterceptor(contextRequestHeaders, businessRequestHeaders);
    }

    @Bean
    @ConditionalOnMissingBean
    public ServiceStrategyRouteFilter serviceStrategyRouteFilter() {
        return new DefaultServiceStrategyRouteFilter();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(value = ServiceStrategyConstant.SPRING_APPLICATION_STRATEGY_REST_INTERCEPT_ENABLED, matchIfMissing = true)
    public DiscoveryEnabledAdapter discoveryEnabledAdapter() {
        return new DefaultDiscoveryEnabledAdapter();
    }


//    @Bean
//    @ConditionalOnMissingBean
//    @ConditionalOnProperty(value = StrategyConstant.SPRING_APPLICATION_STRATEGY_TRACE_ENABLED, matchIfMissing = false)
//    public ServiceStrategyTracerInterceptor serviceStrategyTracerInterceptor() {
//        return new ServiceStrategyTracerInterceptor();
//    }

}
